import request from "../utils/request";

// 获取传感器数据列表
export const getSensorData = (params = {}) => {
  const { page = 1, size = 100, deviceId, startTime, endTime } = params;
  const queryParams = { page, size };
  if (deviceId) queryParams.deviceId = deviceId;
  if (startTime) queryParams.startTime = startTime;
  if (endTime) queryParams.endTime = endTime;
  return request({
    url: "/api/sensor-data",
    method: "get",
    params: queryParams
  });
};

// 获取设备最新数据
export const getLatestData = (deviceId) => {
  return request({
    url: "/api/sensor-data/latest",
    method: "get",
    params: { deviceId }
  });
};

// 获取数据统计
export const getDataStatistics = (params = {}) => {
  const { deviceId, startTime, endTime } = params;
  const queryParams = {};
  if (deviceId) queryParams.deviceId = deviceId;
  if (startTime) queryParams.startTime = startTime;
  if (endTime) queryParams.endTime = endTime;
  return request({
    url: "/api/sensor/statistics",
    method: "get",
    params: queryParams
  });
};

// 获取图表数据
export const getChartData = (params = {}) => {
  const { deviceId, type, startTime, endTime, interval = 'hour' } = params;
  const queryParams = { type, interval };
  if (deviceId) queryParams.deviceId = deviceId;
  if (startTime) queryParams.startTime = startTime;
  if (endTime) queryParams.endTime = endTime;
  return request({
    url: "/api/sensor/chart",
    method: "get",
    params: queryParams
  });
};

// 删除传感器数据
export const deleteSensorData = (dataId) => {
  return request({
    url: `/api/sensor/${dataId}`,
    method: "delete"
  });
};

// 获取实时数据
export const getRealTimeData = () => {
  return request({
    url: "/api/sensor/realtime",
    method: "get"
  });
};

// 接收ESP32设备数据
export const receiveDeviceData = (data) => {
  return request({
    url: "/api/sensor-data/receive",
    method: "post",
    data: data
  });
};

// 获取历史数据
export const getHistoryData = (params = {}) => {
  const { deviceId, startTime, endTime, limit = 100 } = params;
  const queryParams = { limit };
  if (deviceId) queryParams.deviceId = deviceId;
  if (startTime) queryParams.startTime = startTime;
  if (endTime) queryParams.endTime = endTime;
  return request({
    url: "/api/monitor/history",
    method: "get",
    params: queryParams
  });
};

// 获取最新监控数据 (兼容旧接口)
export const getLatestMonitorData = (deviceId) => {
  return request({
    url: "/api/latest",
    method: "get",
    params: { deviceId }
  });
};